﻿using BaseUI;
using BLL;
using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Web.UI.WebControls;

namespace Thksoft.Module.ProjectDetails
{
    public partial class OutcomeFive : BasePage
    {
        public decimal loginUserCompanyId = 1;
        private QS_PROJECTDETAILBLL probll = new QS_PROJECTDETAILBLL();

        /// <summary>
        /// 判断是否首次加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                loginUserCompanyId = lognUser.USER_COMPANY;
                //判断是否是只查询
                if (Request.QueryString["Select"] == "sel")
                {
                    //btnSum.Style.Add("display", "none");
                    btnOk.Style.Add("display", "none");
                    gvMonthPlan.Enabled = false;
                    mytr.Attributes.Add("style", "display:none");
                    //btnDaochu.CssClass = "bt_css_s";
                }

                //判断是否可修改
                if (String.Equals(Request.QueryString["Select"], "no", StringComparison.CurrentCultureIgnoreCase))
                {
                    btnOk.CssClass = "hide";
                    mytr.Attributes.Add("class", "hide");
                }

                if (Request.QueryString["Select"] != "sel")
                {
                    gvMonthPlan.Columns[12].Visible = false;
                    //gvMonthPlan.Columns[13].Visible = false;
                }

                switch (Request.QueryString["type"])
                {
                    case "0":
                        //计划申请
                        //   gvMonthPlan.Columns[7].Visible = false;
                        // gvMonthPlan.Columns[8].Visible = false;
                        break;

                    case "1":
                        //单位审批

                        gvMonthPlan.Columns[8].Visible = false;
                        break;

                    case "2":
                        //总部审批
                        // gvMonthPlan.Columns[7].e = false;
                        break;

                    default:
                        break;
                }

                BindInit();
            }
        }

        /// <summary>
        /// 绑定加载数据
        /// </summary>
        public void BindInit()
        {
            var bll = new QS_PLAN_DETAIL3_BLL();
            List<QS_PLAN_DETAIL3> data = null;
            var applyId = Convert.ToInt32(Request.QueryString["ApplyId"]);
            var proId = Convert.ToInt32(Request.QueryString["ProId"]);

            var edit = Request.QueryString["Edit"] != "1" ? false : true;
            switch (Request.QueryString["State"])
            {
                case "1":
                    //data = bll.FindRangeYear3(applyId);
                    break;

                case "2":
                    data = bll.FindRange3(applyId, proId, 3, 2);
                    break;

                case "3":
                    data = bll.FindRange3(applyId, proId, 3, 3);
                    break;

                case "4":
                    if (string.IsNullOrEmpty(Request.QueryString["userId"]))
                    {
                        data = bll.FindRange3(applyId, proId, 3, 4);
                    }
                    else
                    {
                        data = bll.FindRange3(applyId, proId, 3, 4, Request.QueryString["userId"]);
                    }
                    break;

                case "5":
                    if (string.IsNullOrEmpty(Request.QueryString["userId"]))
                    {
                        data = bll.FindRange3(applyId, proId, 3, 5);
                    }
                    else
                    {
                        data = bll.FindRange3(applyId, proId, 3, 5, Request.QueryString["userId"]);
                    }
                    break;

                default:
                    break;
            }

            if (Request.QueryString["Select"] == "sel")
            {
                //查询
                decimal sumGzje = 0;//挂账金额
                decimal sumZbj = 0;//质保金
                decimal sumJe = 0;

                decimal sumdw = 0;
                decimal sumzb = 0;
                for (int i = 0; i < data.Count; i++)
                {
                    QS_PLAN_DETAIL3 _row = data[i];

                    sumGzje += _row.N_GZAMOUNT;

                    sumZbj += _row.N_ZBAMOUNT;

                    sumJe += _row.N_PLANAMOUNT;

                    sumdw += _row.N_REAMOUNT;

                    sumzb += _row.N_SPAMOUNT2;
                }
                data.Add(new QS_PLAN_DETAIL3 { N_TYPE = -1, N_GZAMOUNT = sumGzje, N_ZBAMOUNT = sumZbj, N_PLANAMOUNT = sumJe, N_REAMOUNT = sumdw, N_SPAMOUNT2 = sumzb });//-1
            }
            gvMonthPlan.DataSource = data;
            gvMonthPlan.DataBind();
        }

        //确定
        protected void btnOk_Click(object sender, EventArgs e)
        {
            try
            {
                var entitys = new List<QS_PLAN_DETAIL3>();
                var applyId = Convert.ToInt32(Request.QueryString["ApplyId"]);
                var proId = Convert.ToInt32(Request.QueryString["ProId"]);
                foreach (GridViewRow row in gvMonthPlan.Rows)
                {
                    //判断属于最后一行剔除
                    //if (row.RowIndex == gvMonthPlan.Rows.Count - 1)
                    //{
                    //    continue;
                    //}
                    var c1 = row.Cells[1].Controls[1] as TextBox;
                    var c4 = row.Cells[2].Controls[1] as DropDownList;
                    var c2_1 = row.Cells[3].Controls[1] as TextBox;
                    var c2_2 = row.Cells[3].Controls[3] as HiddenField;
                    var c3 = row.Cells[4].Controls[1] as DropDownList;
                    var c5 = row.Cells[5].Controls[1] as DropDownList;
                    var c6 = row.Cells[6].Controls[1] as TextBox;
                    var c7 = row.Cells[7].Controls[1] as TextBox;
                    var c8 = row.Cells[8].Controls[1] as TextBox;
                    var c9 = row.Cells[9].Controls[1] as TextBox;
                    var c10 = row.Cells[10].Controls[1] as TextBox;
                    var c11 = row.Cells[11].Controls[1] as TextBox;
                    var entity = new QS_PLAN_DETAIL3();
                    if (c1.Text == "")
                    {
                        MessageBox.Show(this, "请先完善第" + (row.RowIndex + 1) + "数据");
                        return;
                    }

                    entity.N_PRO_ID = proId;
                    entity.N_TYPE = decimal.Parse(Request.QueryString["State"].ToString()); //2;
                    entity.N_PLANAPP_ID = applyId;
                    entity.N_NUM = decimal.Parse(c1.Text);
                    entity.C_PRONAME = c4.SelectedItem.Text;
                    entity.N_CUSTOMER_ID = Convert.ToDecimal(c2_2.Value);
                    entity.C_CUSTOMER_NAME = c2_1.Text.ToString();
                    entity.C_AMOUT_TYPE = c3.SelectedItem.Text;
                    entity.C_AMOUT_USE = c5.SelectedItem.Text;

                    entity.N_GZAMOUNT = c6.Text == "" ? 0 : decimal.Parse(c6.Text);
                    entity.N_ZBAMOUNT = c7.Text == "" ? 0 : decimal.Parse(c7.Text);

                    entity.N_PLANAMOUNT = c8.Text == "" ? 0 : decimal.Parse(c8.Text);
                    //    entity.N_REAMOUNT = c8.Text == "" ? 0 : decimal.Parse(c8.Text);

                    entity.N_REAMOUNT = c9.Text == "" ? 0 : decimal.Parse(c9.Text);
                    entity.N_SPAMOUNT2 = c10.Text == "" ? 0 : decimal.Parse(c10.Text);

                    if (Request.QueryString["type"] == "0")
                    {
                        //计划上报
                        entity.N_REAMOUNT = c8.Text == "" ? 0 : decimal.Parse(c8.Text);//计划取值数据
                    }
                    else if (Request.QueryString["type"] == "1")
                    {
                        //单位审批
                        if (entity.N_REAMOUNT != 0)
                        {
                            entity.N_REAMOUNT = entity.N_REAMOUNT;//计划取值数据
                        }
                        else
                        {
                            //单位未审批，取计划
                            entity.N_REAMOUNT = entity.N_PLANAMOUNT;
                        }
                    }
                    else if (Request.QueryString["type"] == "2")
                    {
                        //总部审批
                        if (entity.N_SPAMOUNT2 != 0)
                        {
                            entity.N_REAMOUNT = entity.N_SPAMOUNT2;//
                        }
                        else if (entity.N_REAMOUNT != 0)
                        {
                            entity.N_REAMOUNT = entity.N_REAMOUNT;
                        }
                        else
                        {
                            //都未审批，取计划
                            entity.N_REAMOUNT = entity.N_PLANAMOUNT;
                        }
                    }

                    entity.C_REMARK = c11.Text;

                    entity.N_PROTYPE = 3;

                    entitys.Add(entity);
                }
                var bll = new QS_PLAN_DETAIL3_BLL();

                switch (Request.QueryString["State"])
                {
                    case "1":
                        //bll.UpdateOrCreateRangeYear5(applyId, entitys);
                        break;

                    case "2":
                        bll.UpdateOrCreateRange3(applyId, proId, 3, 2, entitys);
                        break;

                    case "3":
                        bll.UpdateOrCreateRange3(applyId, proId, 3, 3, entitys);
                        break;

                    case "4":
                        bll.UpdateOrCreateRange3(applyId, proId, 3, 4, entitys);
                        break;

                    case "5":
                        bll.UpdateOrCreateRange3(applyId, proId, 3, 5, entitys);
                        break;

                    default:
                        break;
                }
                MessageBox.ResponseScript(this, "app1.ReLoad();");

                MessageBox.ResponseScript(this, "FORM_CLOSE();");
                return;
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, "提交失败");
            }
        }

        //求和
        protected void btnSum_Click(object sender, EventArgs e)
        {
            //Sum();
        }

        //设置样式
        protected void gvMonthPlan_RowDataBound1(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex == -1)
            {
                return;
            }

            #region 绑定下拉列表框数据

            //公司弹出框ID设定
            if ((HiddenField)e.Row.FindControl("FORM_COM_IDS") != null && (TextBox)e.Row.FindControl("FORM_COM_STRING") != null)
            {
                TextBox tbx = (TextBox)e.Row.FindControl("FORM_COM_STRING");
                HiddenField hf = (HiddenField)e.Row.FindControl("FORM_COM_IDS");
                tbx.Attributes.Add("OnClick", "CheckCustomer(" + tbx.ClientID + "," + hf.ClientID + ")");
                hf.Value = "-1";
            }

            //客户公司
            int count;
            string where = lognUser.USER_COMPANY.ToString();
            string orderby = " N_ID ";
            var dt = new Yx_CustomerBLL().GetEntitys(where, orderby, 0, 1000, out count);

            if (((DropDownList)e.Row.FindControl("FORM_PayeeCompany")) != null)
            {
                DropDownList ddl = (DropDownList)e.Row.FindControl("FORM_PayeeCompany");
                ddl.Items.Clear();
                if (dt != null)
                {
                    for (int i = 0; i < dt.Count; i++)
                    {
                        ddl.Items.Insert(0, new ListItem(dt[i].C_NAME.ToString(), dt[i].N_ID.ToString()));
                    }
                }
                // ddl.Items.Insert(0, new ListItem("---请选择---", "-1"));
            }

            //款项类别
            var where2 = string.Format(@" AND C_TABLETYPE = '{0}' AND N_NAMETYPE = {1} ", "T5", 3);
            var dt2 = new QS_PlanDetail_DropLIstBLL().SelectPlanDropList(where2);

            if (((DropDownList)e.Row.FindControl("FORM_PaymentCategory")) != null)
            {
                DropDownList ddl = (DropDownList)e.Row.FindControl("FORM_PaymentCategory");
                ddl.Items.Clear();
                if (dt2 != null)
                {
                    for (int i = 0; i < dt2.Rows.Count; i++)
                    {
                        ddl.Items.Insert(0, new ListItem(dt2.Rows[i][1].ToString(), dt2.Rows[i][0].ToString()));
                    }
                }
                // ddl.Items.Insert(0, new ListItem("---请选择---", "-1"));
            }

            //款项用途
            var where3 = string.Format(@" AND C_TABLETYPE = '{0}' AND N_NAMETYPE = {1} ", "T5", 4);
            var dt3 = new QS_PlanDetail_DropLIstBLL().SelectPlanDropList(where3);

            if (((DropDownList)e.Row.FindControl("FORM_PaymentPurpose")) != null)
            {
                DropDownList ddl = (DropDownList)e.Row.FindControl("FORM_PaymentPurpose");
                ddl.Items.Clear();
                if (dt3 != null)
                {
                    for (int i = 0; i < dt3.Rows.Count; i++)
                    {
                        ddl.Items.Insert(0, new ListItem(dt3.Rows[i][1].ToString(), dt3.Rows[i][0].ToString()));
                    }
                }
                // ddl.Items.Insert(0, new ListItem("---请选择---", "-1"));
            }

            //项目名称
            var proId = Convert.ToInt32(Request.QueryString["ProId"]);
            var where4 = string.Format(@" AND C_TABLETYPE = '{0}' AND N_NAMETYPE = {1} ", "T5", 1);
            var dt4 = new QS_PlanDetail_DropLIstBLL().SelectPlanDropList(where4);

            if (((DropDownList)e.Row.FindControl("FORM_ProjectName")) != null)
            {
                DropDownList ddl = (DropDownList)e.Row.FindControl("FORM_ProjectName");
                ddl.Items.Clear();
                if (dt4 != null)
                {
                    for (int i = 0; i < dt4.Rows.Count; i++)
                    {
                        ddl.Items.Insert(0, new ListItem(dt4.Rows[i][1].ToString(), dt4.Rows[i][0].ToString()));
                    }
                }
                // ddl.Items.Insert(0, new ListItem("---请选择---", "-1"));
            }

            #endregion 绑定下拉列表框数据

            QS_PLAN_DETAIL3 _row = e.Row.DataItem as QS_PLAN_DETAIL3;
            var row = e.Row;
            if (_row.N_TYPE == -1)//如果数据为Null则代表为合计行
            {
                row.Cells[1].Text = "";
                row.Cells[2].Text = "合计";
                row.Cells[3].Text = "";

                row.Cells[4].Text = "";
                row.Cells[5].Text = "";
                row.Cells[6].Text = _row.N_GZAMOUNT.ToString();
                row.Cells[7].Text = _row.N_ZBAMOUNT.ToString();
                row.Cells[8].Text = _row.N_PLANAMOUNT.ToString();
                row.Cells[9].Text = _row.N_REAMOUNT.ToString();
                row.Cells[10].Text = _row.N_SPAMOUNT2.ToString();
                row.Cells[11].Text = "";
                return;
            }
            if (_row.N_TYPE == -2)
            {
                var cc1 = row.Cells[1].Controls[1] as TextBox;
                cc1.Text = _row.N_NUM.ToString();

                var a8 = row.Cells[8].Controls[1] as TextBox;
                var a9 = row.Cells[9].Controls[1] as TextBox;
                var a10 = row.Cells[10].Controls[1] as TextBox;

                if (Request.QueryString["type"] == "2")
                {
                    //总部审核 ，不能更改单位审批和计划
                    a9.Enabled = true;
                    a8.Enabled = false;
                }
                else if (Request.QueryString["type"] == "1")
                {
                    a8.Enabled = false;
                }
                else if (Request.QueryString["type"] == "0")
                {
                    a9.Enabled = false;
                    a10.Enabled = false;
                }
                return;
            }
            var c1 = row.Cells[1].Controls[1] as TextBox;
            var c4 = row.Cells[2].Controls[1] as DropDownList;
            var c2_1 = row.Cells[3].Controls[1] as TextBox;
            var c2_2 = row.Cells[3].Controls[3] as HiddenField;
            var c3 = row.Cells[4].Controls[1] as DropDownList;

            var c5 = row.Cells[5].Controls[1] as DropDownList;
            var c6 = row.Cells[6].Controls[1] as TextBox;
            var c7 = row.Cells[7].Controls[1] as TextBox;
            var c8 = row.Cells[8].Controls[1] as TextBox;
            var c9 = row.Cells[9].Controls[1] as TextBox;
            var c10 = row.Cells[10].Controls[1] as TextBox;
            var c11 = row.Cells[11].Controls[1] as TextBox;

            if (Request.QueryString["type"] == "2")
            {
                //总部审核 ，不能更改单位审批和计划
                c9.Enabled = true;
                c8.Enabled = false;
            }
            else if (Request.QueryString["type"] == "1")
            {
                c8.Enabled = false;
            }
            else if (Request.QueryString["type"] == "0")
            {
                c9.Enabled = false;
                c10.Enabled = false;
            }

            c1.Text = _row.N_NUM.ToString();
            try
            {
                c4.Items.FindByText(_row.C_PRONAME).Selected = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, "序号【" + _row.N_NUM + "】中项目名称原本的【" + _row.C_PRONAME + "】内容已不存在，请重新选择！");
            }
            c2_1.Text = _row.C_CUSTOMER_NAME;
            c2_2.Value = _row.N_CUSTOMER_ID.ToString();

            try
            {
                c3.Items.FindByText(_row.C_AMOUT_TYPE).Selected = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, "序号【" + _row.N_NUM + "】中项目名称原本的【" + _row.C_AMOUT_TYPE + "】内容已不存在，请重新选择！");
            }
            try
            {
                c5.Items.FindByText(_row.C_AMOUT_USE).Selected = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, "序号【" + _row.N_NUM + "】中项目名称原本的【" + _row.C_AMOUT_USE + "】内容已不存在，请重新选择！");
            }

            c6.Text = _row.N_GZAMOUNT == 0 ? "" : _row.N_GZAMOUNT.ToString();
            c7.Text = _row.N_ZBAMOUNT == 0 ? "" : _row.N_ZBAMOUNT.ToString();
            c8.Text = _row.N_PLANAMOUNT == 0 ? "" : _row.N_PLANAMOUNT.ToString();

            c9.Text = _row.N_REAMOUNT == 0 ? "" : _row.N_REAMOUNT.ToString();
            c10.Text = _row.N_SPAMOUNT2 == 0 ? "" : _row.N_SPAMOUNT2.ToString();
            c11.Text = _row.C_REMARK;

            if (String.Equals(Request.QueryString["Select"], "sel", StringComparison.CurrentCultureIgnoreCase))
            {
                var c12 = row.Cells[12].Controls[1] as Label;
                //var c13 = row.Cells[13].Controls[1] as Label;
                //c12.Text = _row.N_AMT == 0 ? "" : _row.N_AMT.ToString();
                c12.Text = _row.C_AMTPRO;
            }
        }

        #region 添加行

        /// <summary>
        /// 添加行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            var dt = new List<QS_PLAN_DETAIL3>();

            //递归把数据从页面上抓取
            foreach (GridViewRow row in gvMonthPlan.Rows)
            {
                //判断属于最后一行剔除
                //if (row.RowIndex == gvMonthPlan.Rows.Count - 1)
                //{
                //    continue;
                //}
                var c1 = row.Cells[1].Controls[1] as TextBox;
                var c4 = row.Cells[2].Controls[1] as DropDownList;
                var c2_1 = row.Cells[3].Controls[1] as TextBox;
                var c2_2 = row.Cells[3].Controls[3] as HiddenField;
                var c3 = row.Cells[4].Controls[1] as DropDownList;
                var c5 = row.Cells[5].Controls[1] as DropDownList;
                var c6 = row.Cells[6].Controls[1] as TextBox;
                var c7 = row.Cells[7].Controls[1] as TextBox;
                var c8 = row.Cells[8].Controls[1] as TextBox;
                var c9 = row.Cells[9].Controls[1] as TextBox;

                var c10 = row.Cells[10].Controls[1] as TextBox;
                var c11 = row.Cells[11].Controls[1] as TextBox;

                var entity = new QS_PLAN_DETAIL3();
                if (c1.Text == "")
                {
                    MessageBox.Show(this, "请先完善第" + (row.RowIndex + 1) + "数据");
                    return;
                }

                entity.N_NUM = decimal.Parse(c1.Text);
                entity.C_PRONAME = c4.SelectedItem.Text == "" ? null : c4.SelectedItem.Text;
                entity.N_CUSTOMER_ID = Convert.ToDecimal(c2_2.Value);
                entity.C_CUSTOMER_NAME = c2_1.Text.ToString();
                entity.C_AMOUT_TYPE = c3.SelectedItem.Text == "" ? null : c3.SelectedItem.Text;
                entity.C_AMOUT_USE = c5.SelectedItem.Text == "" ? null : c5.SelectedItem.Text;
                entity.N_GZAMOUNT = c6.Text == "" ? 0 : decimal.Parse(c6.Text);
                entity.N_ZBAMOUNT = c7.Text == "" ? 0 : decimal.Parse(c7.Text);
                entity.N_PLANAMOUNT = c8.Text == "" ? 0 : decimal.Parse(c8.Text);
                entity.N_REAMOUNT = c9.Text == "" ? 0 : decimal.Parse(c9.Text);
                entity.N_SPAMOUNT2 = c10.Text == "" ? 0 : decimal.Parse(c10.Text);
                entity.C_REMARK = c11.Text;
                dt.Add(entity);
            }

            dt.Add(new QS_PLAN_DETAIL3 { N_NUM = gvMonthPlan.Rows.Count + 1, N_TYPE = -2 });
            gvMonthPlan.DataSource = dt;
            gvMonthPlan.DataBind();
        }

        #endregion 添加行

        #region 删除行

        /// <summary>
        /// 删除行（通过行的索引删除,如果索引为空则删除最后一行）
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDel_Click(object sender, EventArgs e)
        {
            var dt = new List<QS_PLAN_DETAIL3>();
            //递归把数据从页面上抓取
            foreach (GridViewRow row in gvMonthPlan.Rows)
            {
                //判断属于最后一行剔除
                //if (row.RowIndex == gvMonthPlan.Rows.Count - 1)
                //{
                //    continue;
                //}
                var c1 = row.Cells[1].Controls[1] as TextBox;
                var c4 = row.Cells[2].Controls[1] as DropDownList;
                var c2_1 = row.Cells[3].Controls[1] as TextBox;
                var c2_2 = row.Cells[3].Controls[3] as HiddenField;
                var c3 = row.Cells[4].Controls[1] as DropDownList;
                var c5 = row.Cells[5].Controls[1] as DropDownList;
                var c6 = row.Cells[6].Controls[1] as TextBox;
                var c7 = row.Cells[7].Controls[1] as TextBox;
                var c8 = row.Cells[8].Controls[1] as TextBox;
                var c9 = row.Cells[9].Controls[1] as TextBox;

                var c10 = row.Cells[10].Controls[1] as TextBox;
                var c11 = row.Cells[11].Controls[1] as TextBox;

                var entity = new QS_PLAN_DETAIL3();

                entity.N_NUM = decimal.Parse(c1.Text);
                entity.C_PRONAME = c4.SelectedItem.Text;
                entity.N_CUSTOMER_ID = Convert.ToDecimal(c2_2.Value);
                entity.C_CUSTOMER_NAME = c2_1.Text.ToString();
                entity.C_AMOUT_TYPE = c3.SelectedItem.Text;
                entity.C_AMOUT_USE = c5.SelectedItem.Text;
                entity.N_GZAMOUNT = c6.Text == "" ? 0 : decimal.Parse(c6.Text);
                entity.N_ZBAMOUNT = c7.Text == "" ? 0 : decimal.Parse(c7.Text);

                entity.N_PLANAMOUNT = c8.Text == "" ? 0 : decimal.Parse(c8.Text);

                entity.N_REAMOUNT = c9.Text == "" ? 0 : decimal.Parse(c9.Text);
                entity.N_SPAMOUNT2 = c10.Text == "" ? 0 : decimal.Parse(c10.Text);
                entity.C_REMARK = c11.Text;
                dt.Add(entity);
            }
            // dt.Add(new QS_PLAN_DETAIL3 { VALUE1 = "-1" });
            if (dt.Count < 1)
            {
                return;
            }
            int count = 0;
            for (int i = 0; i < gvMonthPlan.Rows.Count; i++)
            {
                if (((CheckBox)gvMonthPlan.Rows[i].Cells[0].FindControl("chkSelectId")).Checked == true)
                {
                    CheckBox cb = ((CheckBox)gvMonthPlan.Rows[i].Cells[0].FindControl("chkSelectId")) as CheckBox;
                    GridViewRow row = cb.NamingContainer as GridViewRow;
                    int id = row.RowIndex;

                    if (id > 0)
                    {
                        id = id - count;
                    }

                    dt.RemoveAt(id);
                    count++;
                }
            }
            //if (p_id.Value == "")
            //{
            //    //删除最后一行
            //    dt.RemoveAt(dt.Count - 1);
            //}
            //else
            //{
            //    if (Convert.ToInt32(p_id.Value) > 0 && Convert.ToInt32(p_id.Value) <= dt.Count)
            //    {
            //        dt.RemoveAt(Convert.ToInt32(p_id.Value)-1);
            //    }

            //}
            gvMonthPlan.DataSource = dt;
            gvMonthPlan.DataBind();
        }

        #endregion 删除行

        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDaochu_Click(object sender, EventArgs e)
        {
            var bll = new QS_PLAN_DETAIL3_BLL();
            DataTable data = null;
            var applyId = Convert.ToInt32(Request.QueryString["ApplyId"]);
            var proId = Convert.ToInt32(Request.QueryString["ProId"]);

            switch (Request.QueryString["State"])
            {
                case "1":
                    //data = bll.FindRangeYear3(applyId);
                    break;

                case "2":
                    data = bll.ExcelData(applyId, proId, 2, 3);
                    break;

                case "3":
                    data = bll.ExcelData(applyId, proId, 3, 3);
                    break;

                case "4":
                    data = bll.ExcelData(applyId, proId, 4, 3);
                    break;

                case "5":
                    data = bll.ExcelData(applyId, proId, 5, 3);
                    break;

                default:
                    break;
            }
            toExecl1(data, DateTime.Now.ToString("yyyy-MM-dd") + "大修费用明细表");
        }
    }
}